package com.android.takegism.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.takegism.R;
import com.android.takegism.utils.Clue;
import com.android.takegism.utils.SensorUiItem;

import java.util.LinkedList;

public class SensorUiItemAdapter extends BaseAdapter {

    private Context mContext;
    private LinkedList<SensorUiItem> mData;

    public SensorUiItemAdapter() {
    }

    public SensorUiItemAdapter(LinkedList<SensorUiItem> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    public LinkedList<SensorUiItem> getmData() {
        if (mData == null) {
            this.mData = new LinkedList<>();
        }
        return mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.sensor_ui_item, parent, false);
            holder = new ViewHolder();
            holder.img_icon = convertView.findViewById(R.id.sensor_item_image);
            holder.s1 = convertView.findViewById(R.id.sensor_item_s1);
            holder.s2 = convertView.findViewById(R.id.sensor_item_s2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        System.out.println(holder.img_icon);
        System.out.println(holder.s1);
        System.out.println(holder.s2);


        holder.img_icon.setAdjustViewBounds(true);
        holder.img_icon.setMaxWidth(180);
        holder.img_icon.setMaxHeight(200);
        holder.img_icon.setImageResource(mData.get(position).getImageId());
        holder.s1.setTextSize(18);
        holder.s1.setText(mData.get(position).getType());
        holder.s2.setTextSize(18);
        holder.s2.setTextColor(R.color.green);
        holder.s2.setText(mData.get(position).getData());

        return convertView;
    }

    //添加一个元素
    public void add(SensorUiItem data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, SensorUiItem data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void changed(){
        notifyDataSetChanged();
    }
    public void remove(Clue data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }

    private class ViewHolder {
        ImageView img_icon;
        TextView s1;
        TextView s2;
    }

}